package crypto

import (
	"blueprint-crypto/internal/util"
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

// 批量加密
func BatchRSA_Encrypt(rootPath string, pempath string, keyword string, newKeyword string) {
	if !util.PathExists(rootPath) {
		panic(rootPath + "路径不存在，请检查")
	}
	if !util.PathExists(pempath) {
		panic(pempath + "文件不存在，请检查")
	}
	plainFiles := util.Getfilepath(rootPath, keyword)
	for _, plainFile := range plainFiles {
		if util.IsFile(plainFile) {
			paths, fileName := filepath.Split(plainFile)
			if strings.Contains(fileName, keyword) {
				newFilename := strings.ReplaceAll(fileName, keyword, newKeyword)
				fmt.Println("源文件：", plainFile, " 加密后文件：", paths+newFilename)
				RSA_Encrypt(plainFile, pempath, paths+newFilename)
				//加密完成后删除原文件
				err := os.RemoveAll(plainFile)
				if err != nil {
					fmt.Errorf("明文文件删除失败")
				}
				fmt.Println("源文件：", plainFile, "已经移除")
			}

		}
	}
}
